package com.lishe.lishe.lishe.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.lishe.lishe.lishe.base.BaseApplication;
import com.lishe.lishe.lishe.common.Globals;

import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by zhcase on 2017/9/5.
 */

public class SharedPreferencesUtils {

    private static final String PrefName = "UserPref";
    private static final String UserName_KEY = "userName";
    private static final String Password_KEY = "password";
    private static final String UserId_KEY = "userId";
    private static final String Token_KEY = "token";
    private static final int comSize = -1;//企业个数
    /**
     * 保存激励模块个人中心信息
     */
    private static final String Excitation_person_img = "excitationPersonImg";
    private static final String Excitation_person_name = "excitationPersonName";


    /**
     * 保存激励模块用户头像和名字
     *
     * @param context
     * @param imgUrl
     * @param name
     */
    public static void saveExcitationPersonInfo(Context context, String imgUrl, String name) {
        if (imgUrl == null && name == null) {
            return;
        }
        SharedPreferences sharedPreferences = BaseApplication.getContext().getSharedPreferences(PrefName, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(Excitation_person_img, imgUrl);
        if (!TextUtils.isEmpty(name)) {
            editor.putString(Excitation_person_name, name);
        }
        editor.commit();// 提交修改
    }

    public static String getExcitationPersonImg(Context context) {
        SharedPreferences sharedPreferences = BaseApplication.getContext().getSharedPreferences(PrefName, MODE_PRIVATE);
        return sharedPreferences.getString(Excitation_person_img, null);
    }

    public static String getExcitationPersonName(Context context) {
        SharedPreferences sharedPreferences = BaseApplication.getContext().getSharedPreferences(PrefName, MODE_PRIVATE);
        return sharedPreferences.getString(Excitation_person_name, null);
    }

    /**
     * 保存token
     */
    public static void saveToken(String token, Context context) {
        Log.e("保存token", token);
        SharedPreferences tokenShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = tokenShare.edit();
        ed.putString("token", token);
        ed.commit();
    }

    /**
     * 获取token
     */
    public static String getToken(Context context) {
        SharedPreferences tokenShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        String token = tokenShare.getString("token", null);
        return token;
    }

    /**
     * 清除token
     */
    public static void removeToken(Context context) {
        Log.e("删除token", "" + Globals.getToken());
        SharedPreferences tokenShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = tokenShare.edit();
        ed.remove("token");
        ed.commit();
    }

    /**
     * 保存userid
     */
    public static void saveUserId(String userId, Context context) {
        SharedPreferences userIdShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = userIdShare.edit();
        ed.putString("userId", userId);
        ed.commit();
    }

    /**
     * 清除userid
     */
    public static String getUserId(Context context) {
        SharedPreferences userIdShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return userIdShare.getString("userId", null);
    }

    /**
     * 清除userid
     */
    public static void removeUserId(Context context) {
        SharedPreferences userIdShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = userIdShare.edit();
        ed.remove("userId");
        ed.commit();
    }

    /**
     * 保存手机号
     */
    public static void savePhone(String phone, Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = phoneShare.edit();
        ed.putString("phone", phone);
        ed.commit();
    }

    /**
     * 保存手机号
     */
    public static String getPhone(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getString("phone", null);
    }

    /**
     * 清除手机号
     */
    public static void removePhone(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = phoneShare.edit();
        ed.remove("phone");
        ed.commit();
    }

    /**
     * 保存激励模块userId
     */
    public static void saveExcitationUserId(String userId, Context context) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("excitationUserId", userId);
        ed.commit();
    }

    /**
     * 获取激励模块UserId
     */
    public static String getExcitationUserId(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getString("excitationUserId", null);
    }

    /**
     * 保存企业数
     */
    public static void saveComSize(int comSize, Context context) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putInt("comSize", comSize);
        ed.commit();
    }

    /**
     * 获取激励模块UserId
     */
    public static int getComSize(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getInt("comSize", -1);
    }

    /**
     * 清除手机号
     */
    public static void removeComSize(Context context) {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = phoneShare.edit();
        ed.remove("comSize");
        ed.commit();
    }

    /**
     * 保存极光推送RegistrationID
     */
    public static void saveRegistrationID(String RegistrationID) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("RegistrationID", RegistrationID);
        ed.commit();
    }

    /**
     * 获取极光推送RegistrationID
     */
    public static String getRegistrationID() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getString("RegistrationID", null);
    }

    /**
     * 保存从服务器获取的新的版本信息
     */
    public static void saveAppVersionAndCode(String app_version, int app_code) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("app_version", app_version);
        ed.putInt("app_code", app_code);
        ed.commit();
    }

    public static int getAppCode() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getInt("app_code", 0);
    }

    public static String getAppVersion() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getString("app_version", null);
    }

    /**
     * 保存通告id
     */
    public static void saveNoticeID(String noticeId) {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("notice_id", getNoticeID() + noticeId + ",");
        ed.commit();
    }

    public static String getNoticeID() {
        SharedPreferences phoneShare = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        return phoneShare.getString("notice_id", "");
    }

    /**
     * 清除通告记录
     */
    public static void clearNoticeID() {
        SharedPreferences share = BaseApplication.getContext().getSharedPreferences(Globals.SHAREPREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = share.edit();
        ed.putString("notice_id", "");
        ed.commit();
    }
}

